﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BestBuy.DAL
{
    public class cDangSanPham
    {
        //private TradeKeyDataContext context = new TradeKeyDataContext();
        public static String GetCategoryString(int ID)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            string str = "";

            DanhMuc lv1 = context.DanhMucs.Where(u => u.IDDanhMuc == ID).FirstOrDefault();
            DanhMuc lv2 = null;
            DanhMuc lv3 = null;

            if (lv1 != null)
            {
                lv2 = context.DanhMucs.Where(u => u.IDDanhMuc == lv1.IDDanhMucCha).FirstOrDefault();
                if (lv2 != null)
                {
                    lv3 = context.DanhMucs.Where(u => u.IDDanhMuc == lv2.IDDanhMucCha).FirstOrDefault();
                }
            }
            if (lv3 != null)// co 3 cap
            {
                str = lv3.TenDanhMuc + " >> " + lv2.TenDanhMuc + " >> " + lv1.TenDanhMuc;
            }
            else
            {
                if (lv2 != null)// co 2 cap
                {
                    str = lv2.TenDanhMuc + " >> " + lv1.TenDanhMuc;
                }
                else// co 1 cap
                {
                    str = lv1.TenDanhMuc;
                }

            }

            return str;
        }
        public static int countChildren(int idSP)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMucCha == idSP).Count();            
        }
        public static List<DanhMuc> loadChildren(int idsp)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMucCha == idsp).ToList();
        }
        public static String getNameProduct(int idSP)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            return context.DanhMucs.Where(p => p.IDDanhMuc == idSP).FirstOrDefault().TenDanhMuc;
        }
        public static List<DanhMuc> GetChildrenOfCategory(int? parentID)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            List<DanhMuc> list = new List<DanhMuc>();

            if (parentID.HasValue)
            {
                list = context.DanhMucs.Where(p => p.IDDanhMucCha == parentID).ToList();
            }
            else
            {
                list = context.DanhMucs.Where(p => !p.IDDanhMucCha.HasValue).ToList();
            }

            return list;
        }
        public static void AddProduct(SanPham product)
        {
            TradeKeyDataContext context = new TradeKeyDataContext();
            context.SanPhams.InsertOnSubmit(product);
            context.SubmitChanges();
        }
    }
}